package com.leo.quartz.service.impl;

import com.leo.base.AbstractService;
import com.leo.base.BaseResponse;
import com.leo.base.PageResponse;
import com.leo.base.ReturnResponse;
import com.leo.quartz.common.lang.ReturnCodeConstant;
import com.leo.quartz.common.request.MyJobRequestParam;
import com.leo.quartz.common.response.task.MyJobVO;
import com.leo.quartz.entity.QuartzJobPO;
import com.leo.quartz.repository.JobRepository;
import com.leo.quartz.service.JobService;
import com.leo.util.PoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liusl
 * @version 1.0
 * @date 2018/12/8 17:23
 */
@Service
public class JobServiceImpl extends AbstractService implements JobService {

    private JobRepository jobRepository;

    @Autowired
    public JobServiceImpl(JobRepository jobRepository) {
        this.jobRepository = jobRepository;
    }


    @Override
    public PageResponse<MyJobVO> queryMyJobList(MyJobRequestParam params) {
        Specification<QuartzJobPO> specification = buildSpecification(params, QuartzJobPO.class);
        Page<QuartzJobPO> pages = jobRepository.findAll(specification, params.buildPageRequest());
        List<MyJobVO> vos = PoUtils.copyBeans(pages.getContent(), MyJobVO.class);
        return new PageResponse<>(ReturnCodeConstant.SUCCESS, buildResponseStatus(), pages.getTotalElements(), vos);
    }

    @Override
    public ReturnResponse removeJob(Long id) {
        jobRepository.deleteById(id);
        return buildSuccess();
    }

    @Override
    public BaseResponse<MyJobVO> getJobById(Long id) {
        QuartzJobPO po = jobRepository.getOne(id);
        MyJobVO vo = PoUtils.copyBean(po, MyJobVO.class);
        return new BaseResponse<>(buildSuccess(), vo);
    }

    @Override
    public ReturnResponse editJob(MyJobRequestParam param) {
        QuartzJobPO po = jobRepository.getOne(param.getId());
        if (StringUtils.compare(po.getJobClass(), param.getJobClass()) != 0) {
            po.setJobClass(param.getJobClass());
        }
        if (StringUtils.compare(po.getJobDesc(), param.getJobDesc()) != 0) {
            po.setJobDesc(param.getJobDesc());
        }
        if (StringUtils.compare(po.getJobGroup(), param.getGroupName()) != 0) {
            po.setJobGroup(param.getGroupName());
        }
        if (StringUtils.compare(po.getJobName(), param.getJobName()) != 0) {
            po.setJobName(param.getJobName());
        }
        if (po.getJobStatus() != QuartzJobPO.JobStatusEnum.valueOf(param.getJobStatus())) {
            po.setJobClass(param.getJobClass());
        }
        jobRepository.save(po);
        return buildSuccess();
    }

    @Override
    public ReturnResponse addJob(MyJobRequestParam param) {
        QuartzJobPO po = new QuartzJobPO();
        po.setJobName(param.getJobName());
        po.setJobDesc(param.getJobDesc());
        po.setJobGroup(param.getGroupName());
        po.setJobClass(param.getJobClass());
        po.setJobStatus(QuartzJobPO.JobStatusEnum.valueOf(param.getJobStatus()));
        Date now = new Date();
        po.setCreateTime(now);
        po.setUpdateTime(now);
        jobRepository.save(po);
        return buildSuccess();
    }
}
